<?php

namespace Emergine\Libs
{
/**
   * lib.models -- Emergine Models Library
   * @version: 1.0
   * @author: Hleb Piatrou <h.piatrou@softteco.com>
   * @link: http://code.google.com/p/emergine/
   * @copyright: Copyright 2011-2012 Hleb Piatrou
   * @license: MIT License http://www.opensource.org/licenses/mit-license.php
   * @interface: Common Control Interface (CCI)
   */

/**
   * Emergine Highlight class
   * 
   * This class can be used to highlight programming languages
   * 
   * Example of usage:
   * <code>
   * $highlighted_code = Hightlight::SQL($code);
   * echo $highlighted_code;
   * </code>
   * @interface: Control
   */
   
   class Highlight
   {
	   /**
		 * @access: protected
		 * @param: none
		 * @return: array
		 */	
	   protected function Colors()
	   {
		   $colors = Array('chars' => 'grey', 'keywords' => 'blue', 'joins' => 'gray', 'functions' => 'violet', 'constants' => 'red');
		   return $colors;
	   }
	   /**
		 * @access: protected
		 * @param: $key
		 * @return: array
		 */	
		 protected function Words ($key)
		 {
			 $words = Array ('keywords' =>
			 array('SELECT', 'UPDATE', 'INSERT', 'DELETE', 'REPLACE', 'INTO', 'CREATE', 'ALTER', 'TABLE', 'DROP', 'TRUNCATE', 'FROM',
			 'ADD', 'CHANGE', 'COLUMN', 'KEY',
			 'WHERE', 'ON', 'CASE', 'WHEN', 'THEN', 'END', 'ELSE', 'AS', 
			 'USING', 'USE', 'INDEX', 'CONSTRAINT', 'REFERENCES', 'DUPLICATE',
			 'LIMIT', 'OFFSET', 'SET', 'SHOW', 'STATUS', 
			 'BETWEEN', 'AND', 'IS', 'NOT', 'OR', 'XOR', 'INTERVAL', 'TOP',
			 'GROUP BY', 'ORDER BY', 'DESC', 'ASC', 'COLLATE', 'NAMES', 'UTF8', 'DISTINCT', 'DATABASE',
			 'CALC_FOUND_ROWS', 'SQL_NO_CACHE', 'MATCH', 'AGAINST', 'LIKE', 'REGEXP', 'RLIKE',
			 'PRIMARY', 'AUTO_INCREMENT', 'DEFAULT', 'IDENTITY', 'VALUES', 'PROCEDURE', 'FUNCTION', 
			 'TRAN', 'TRANSACTION', 'COMMIT', 'ROLLBACK', 'SAVEPOINT', 'TRIGGER', 'CASCADE',
			 'DECLARE', 'CURSOR', 'FOR', 'DEALLOCATE'),
			 'joins' => array('JOIN', 'INNER', 'OUTER', 'FULL', 'NATURAL', 'LEFT', 'RIGHT'),
			 'chars' => '/([\\.,\\(\\)<>:=`]+)/i',
			 'functions' => array('MIN', 'MAX', 'SUM', 'COUNT', 'AVG', 'CAST', 'COALESCE', 'CHAR_LENGTH', 'LENGTH', 'SUBSTRING',
			 'DAY', 'MONTH', 'YEAR', 'DATE_FORMAT', 'CRC32', 'CURDATE', 'SYSDATE', 'NOW', 'GETDATE',
			 'FROM_UNIXTIME', 'FROM_DAYS', 'TO_DAYS', 'HOUR', 'IFNULL', 'ISNULL', 'NVL', 'NVL2',
			 'INET_ATON', 'INET_NTOA', 'INSTR', 'FOUND_ROWS',
			 'LAST_INSERT_ID', 'LCASE', 'LOWER', 'UCASE', 'UPPER',
			 'LPAD','RPAD','RTRIM','LTRIM',
			 'MD5','MINUTE', 'ROUND',
			 'SECOND', 'SHA1', 'STDDEV', 'STR_TO_DATE', 'WEEK'),
			 'constants' => '/(\'[^\']*\'|[0-9]+)/i'
			 );
			 return $words[$key];
		}
	   /**
		 * @access: public
		 * @param: $colors
		 * @return: none
		 */	
		function __construct($colors = 0) 
		{
			if ($colors) $this->colors = $colors;
		}
	   /**
		 * @access: public
		 * @param: $sql
		 * @return: string
		 */	
		public function SQL($sql)
		{
			$sql = str_replace('\\\'', '\\&#039;', $sql);
			foreach(Highlight::Colors() as $key=>$color)
			{
				if (in_array($key, Array('constants', 'chars'))) 
				{
					$regexp = Highlight::words($key);
				}
				else 
				{
					$regexp = '/\\b(' . join("|", Highlight::words($key)) . ')\\b/i';
				}
				$sql = preg_replace($regexp, '<span style="color:'.$color."\">$1</span>", $sql);
				}
				return $sql;
		}
	}
}
?>
